//
//  HEATPersonListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/2/8.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEATPersonListViewController.h"

@interface HEATPersonListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;

@end

@implementation HEATPersonListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"选择提醒的人";
    
    [self initUI];
}

- (void)initUI {
    [self zx_setLeftBtnWithText:@"取消" clickedBlock:^(UIButton * _Nonnull btn) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.view addSubview:self.tableView];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
                
    }
    return _tableView;
}

- (void)setDataSource:(NSMutableArray<HEIMKitInfo *> *)dataSource {
    NSMutableArray *arr = @[].mutableCopy;
    for (HEIMKitInfo *info in dataSource) {
        if (![info.infoId isEqualToString: [UserManager sharedUserManager].curUserInfo.id]) {
            [arr addObject:info];
        }
    }
    _dataSource = arr;
}

#pragma mark - UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [cell.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(40);
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).offset(15);
        }];
        
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 20;
        
        [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.imageView.mas_right).offset(20);
            make.right.equalTo(cell.contentView).offset(-15);
        }];
    }
    
    HEIMKitInfo *info = _dataSource[indexPath.row];
    [cell.imageView sd_setImageWithURL:info.avatUrl placeholderImage:info.avatarImage];
    cell.textLabel.text = info.showName;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEIMKitInfo *info = _dataSource[indexPath.row];

    if(self.backParamBlock) {
        self.backParamBlock(info);
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
